package org.lora.biz.workflow.engines.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.lora.biz.ou.entity.EmployeeIdentityView;
import org.lora.biz.ou.exception.LoraOrgUserException;
import org.lora.biz.ou.group.service.IGroupService;
import org.lora.biz.workflow.WorkflowConstant;
import org.lora.biz.workflow.engines.ExcuterBean;
import org.lora.biz.workflow.engines.IProcessDefinitionService;
import org.lora.biz.workflow.engines.IProcessMoveService;
import org.lora.biz.workflow.engines.bean.ActivityDefinitionBean;
import org.lora.biz.workflow.engines.bean.PathDefinitionBean;
import org.lora.biz.workflow.engines.bean.ProcessDefinitionBean;
import org.lora.biz.workflow.engines.bean.TaskInstanceBean;
import org.lora.biz.workflow.engines.entity.ProcessInstance;
import org.lora.biz.workflow.engines.entity.ProcessInstanceExample;
import org.lora.biz.workflow.engines.entity.ProcessTask;
import org.lora.biz.workflow.engines.entity.ProcessTaskExample;
import org.lora.biz.workflow.engines.entity.ProcessTaskExcute;
import org.lora.biz.workflow.engines.mapper.ProcessInstanceMapper;
import org.lora.biz.workflow.engines.mapper.ProcessTaskExcuteMapper;
import org.lora.biz.workflow.engines.mapper.ProcessTaskMapper;
import org.lora.biz.workflow.exception.LoraWorkflowException;
import org.lora.core.annotation.AutoAssemble;
import org.lora.core.annotation.Service;
import org.lora.core.annotation.Transaction;
import org.lora.exception.LoraCoreException;
import org.lora.util.StringUtil;
import org.lora.util.ValidateUtil;
import org.lora.webframework.common.FrameWorkBaseService;

/**
 * <p>
 * 类名:ProcessMoveServiceImpl
 * </p>
 * 描述: 流程运行服务实现<br>
 * 创建时间:2016年4月12日 上午10:46:41<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年4月12日 Bladnir 创建<br>
 */
@Service
public class ProcessMoveServiceImpl1 extends FrameWorkBaseService implements IProcessMoveService {

	@AutoAssemble
	private ProcessInstanceMapper processInstanceMapper;

	@AutoAssemble
	private ProcessTaskExcuteMapper processTaskExcuteMapper;

	@AutoAssemble
	private ProcessTaskMapper processTaskMapper;

	@AutoAssemble
	private IProcessDefinitionService processDefinitionService;

	@AutoAssemble
	private IGroupService groupService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.workflow.engines.IProcessMoveService#getProcessTaskById(
	 * java.lang.String)
	 */
	@Override
	public ProcessTask getProcessTaskById(String taskId) throws LoraCoreException {
		ValidateUtil.isNull(taskId, "taskId");

		ProcessTaskExample example = new ProcessTaskExample();
		example.createCriteria().andTaskIdEqualTo(taskId);

		List<ProcessTask> processTaskList = processTaskMapper.selectByExample(example);

		if (ValidateUtil.isEmpty(processTaskList)) {
			return null;
		} else {
			return processTaskList.get(0);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.workflow.engines.IProcessMoveService#getProcessInstanceById
	 * (java.lang.String)
	 */
	@Override
	public ProcessInstance getProcessInstanceById(String processInstanceId) throws LoraCoreException {

		ValidateUtil.isNull(processInstanceId, "processInstanceId");

		ProcessInstanceExample example = new ProcessInstanceExample();
		org.lora.biz.workflow.engines.entity.ProcessInstanceExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(processInstanceId);

		List<ProcessInstance> list = processInstanceMapper.selectByExample(example);

		if (ValidateUtil.isEmpty(list)) {
			return null;
		} else {
			return list.get(0);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.workflow.engines.IProcessMoveService#draft(java.lang.String,
	 * org.lora.biz.workflow.engines.ExcuterBean, java.lang.String)
	 */
	@Override
	@Transaction
	public ProcessInstance draft(String processDefinitionId, ExcuterBean excuterBean, String version) throws LoraCoreException {
		ValidateUtil.isNull(processDefinitionId, "processDefinitionId");
		ValidateUtil.isNull(excuterBean, "excuterBean");

		// 获取流程定义
		ProcessDefinitionBean pdEntity = processDefinitionService.getProcessDefById(processDefinitionId);

		// 获取 当前生效版本 起草环节的环节定义
		ActivityDefinitionBean activityDefinitionBean = processDefinitionService.getProcessDefinitionActivity(pdEntity, null, version);

		long currentTime = System.currentTimeMillis();

		// 创建流程实例对象
		ProcessInstance processInstance = createProcessInstance(currentTime, pdEntity, excuterBean);

		// 创建任务实例对象
		creatProcessTask(currentTime, activityDefinitionBean, excuterBean, pdEntity, processInstance.getId());

		return processInstance;
	}

	/**
	 * 创建流程实例对象
	 * 
	 * @param currentTime
	 * @param pdEntity
	 * @param excuterBean
	 * @return
	 */
	private ProcessInstance createProcessInstance(long currentTime, ProcessDefinitionBean pdEntity, ExcuterBean excuterBean) {

		BigDecimal currentTimeBigDecimal = new BigDecimal(currentTime);
		String processInstanceId = super.creatBeanId(WorkflowConstant.PREFIX_PROCESS_INSTANCE);

		ProcessInstance processInstance = new ProcessInstance();

		processInstance.setCreateTime(currentTimeBigDecimal);
		processInstance.setProcessDefinitionId(pdEntity.getProcessDefinition().getId());
		processInstance.setProcessDefinitionVersion(pdEntity.getEffectiveVer().getVersion());
		processInstance.setId(processInstanceId);
		processInstance.setState(WorkflowConstant.PROCESS_STATE_DRAFT);
		processInstance.setProcessDefinitionName(pdEntity.getProcessDefinition().getDefinitionName());
		processInstance.setDomainId(pdEntity.getProcessDefinition().getDomainId());
		processInstance.setCreateExecuteId(excuterBean.getExecuteId());
		processInstance.setCreateExecuteName(excuterBean.getExecuteName());
		processInstance.setCreateExecuteType(excuterBean.getExecuteType());
		processInstance.setDomainId(pdEntity.getProcessDefinition().getDomainId());
		processInstance.setIsDelete(WorkflowConstant.PROCESS_DEL_STATE_NO);

		// 插入流程实例表
		processInstanceMapper.insert(processInstance);

		return processInstance;

	}

	/**
	 * 创建任务实例对象 并保存
	 * 
	 * @param currentTime
	 * @param activityDefinitionBean
	 * @param excuterBean
	 * @param pdEntity
	 * @param processInstanceId
	 * @return
	 */
	private ProcessTask creatProcessTask(long currentTime, ActivityDefinitionBean activityDefinitionBean, ExcuterBean excuterBean,
			ProcessDefinitionBean pdEntity, String processInstanceId) {
		BigDecimal currentTimeBigDecimal = new BigDecimal(currentTime);
		String taskId = super.creatBeanId(WorkflowConstant.PREFIX_TASK);
		ProcessTask processTask = new ProcessTask();

		processTask.setActivityDefId(activityDefinitionBean.getId());
		processTask.setActivityName(activityDefinitionBean.getActName());
		processTask.setActivityNo(activityDefinitionBean.getActNo());
		processTask.setCreateTime(currentTimeBigDecimal);
		processTask.setDomainId(pdEntity.getProcessDefinition().getDomainId());
		processTask.setExecuteAccountId(excuterBean.getExecuteAccountId());
		processTask.setExecuteId(excuterBean.getExecuteId());
		processTask.setExecuteName(excuterBean.getExecuteName());
		processTask.setExecuteType(excuterBean.getExecuteType());
		processTask.setGrantorAccountId(excuterBean.getGrantorAccountId());
		processTask.setGrantorName(excuterBean.getGrantorName());
		processTask.setGrantorReason(excuterBean.getGrantorReason());
		processTask.setGrantorType(excuterBean.getGrantorType());
		processTask.setIsDelete(WorkflowConstant.PROCESS_DEL_STATE_NO);
		processTask.setIsOpen(WorkflowConstant.PROCESS_DEL_STATE_YES);
		processTask.setProcessInstanceId(processInstanceId);
		processTask.setTaskId(taskId);
		processTask.setTaskState(WorkflowConstant.TASK_STATE_TODO);

		processTaskMapper.insert(processTask);

		return processTask;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.workflow.engines.IProcessMoveService#getProcessInfo(java
	 * .lang.String, java.lang.String)
	 */
	@Override
	public TaskInstanceBean getTaskInstance(String processInstanceId, String taskId, boolean isFilterPath, boolean isAddExcute)
			throws LoraCoreException {

		ValidateUtil.isNull(processInstanceId, "processInstanceId");
		ValidateUtil.isNull(taskId, "taskId");

		// 获取流程实例
		ProcessInstance processInstance = this.getProcessInstanceById(processInstanceId);

		if (processInstance == null) {
			throw new LoraWorkflowException("cant find ProcessInstance processInstanceId=[" + processInstanceId + "]");
		}

		// 获取任务实例
		ProcessTask processTask = getProcessTaskById(taskId);

		if (processTask == null) {
			throw new LoraWorkflowException("cant find processTask taskId=[" + taskId + "]");
		}

		// 获取流程定义
		ProcessDefinitionBean pdEntity = processDefinitionService.getProcessDefById(processInstance.getProcessDefinitionId());

		// 获取 当前生效版本 当前任务的环节定义
		ActivityDefinitionBean activityDefinitionBean = processDefinitionService.getProcessDefinitionActivity(pdEntity,
				processTask.getActivityDefId(), processInstance.getProcessDefinitionVersion());

		// 过滤路径
		filterPath(activityDefinitionBean);

		// 添加处理人
		addHandler(activityDefinitionBean, processInstance, processTask);

		TaskInstanceBean result = new TaskInstanceBean();
		result.setActivityDefinitionBean(activityDefinitionBean);
		result.setProcessInstance(processInstance);
		result.setProcessTask(processTask);

		return result;
	}

	/**
	 * 添加处理人
	 * 
	 * @param activityDefinitionBean
	 * @throws LoraOrgUserException
	 */
	private void addHandler(ActivityDefinitionBean activityDefinitionBean, ProcessInstance processInstance, ProcessTask processTask)
			throws LoraOrgUserException {

		List<PathDefinitionBean> nextPathList = activityDefinitionBean.getNextPathList();

		for (PathDefinitionBean pathDefinitionBean : nextPathList) {

			// 后续环节定义
			ActivityDefinitionBean nextActBean = pathDefinitionBean.getToActivityDefinitionBean();

			// 获取后续环节执行者
			List<ExcuterBean> handerList = getExcuterBean(nextActBean, processInstance, processTask);
			nextActBean.setHanderList(handerList);
		}

	}

	/**
	 * 获取后续环节处理人列表
	 * 
	 * @param nextActBean
	 * @param processInstance
	 * @param processTask
	 * @return
	 * @throws LoraOrgUserException
	 */
	private List<ExcuterBean> getExcuterBean(ActivityDefinitionBean nextActBean, ProcessInstance processInstance, ProcessTask processTask)
			throws LoraOrgUserException {
		List<ExcuterBean> result = new ArrayList<>();

		if (ValidateUtil.isEqual(WorkflowConstant.EXCUTER_TYPE_EI, nextActBean.getExcuteType())) {
			// 身份
			String groupId = nextActBean.getMemberGroupId();

			List<EmployeeIdentityView> eiList = groupService.getGroupMember(groupId, processTask.getExecuteId(),
					processInstance.getCreateExecuteId(), null);

			result = packageExcuterBeanList(eiList);

		} else if (ValidateUtil.isEqual(WorkflowConstant.EXCUTER_TYPE_GROUP, nextActBean.getExcuteType())) {
			// 群组

			ExcuterBean bean = new ExcuterBean();

			bean.setExecuteType(WorkflowConstant.EXCUTER_TYPE_GROUP);
			bean.setExecuteName(nextActBean.getGroupName());
			bean.setExecuteId(nextActBean.getGroupId());

			result.add(bean);

		} else if (ValidateUtil.isEqual(WorkflowConstant.EXCUTER_TYPE_ORG, nextActBean.getExcuteType())) {
			// 机构
			ExcuterBean bean = new ExcuterBean();

			bean.setExecuteType(WorkflowConstant.EXCUTER_TYPE_ORG);
			bean.setExecuteId(nextActBean.getOrgId());
			bean.setExecuteName(nextActBean.getOrgName());

			result.add(bean);

		} else if (ValidateUtil.isEqual(WorkflowConstant.EXCUTER_TYPE_OTHER, nextActBean.getExcuteType())) {
			// 其他环节实际处理人
			// TODO
		}

		return result;
	}

	private List<ExcuterBean> packageExcuterBeanList(List<EmployeeIdentityView> eiList) {
		if (ValidateUtil.isEmpty(eiList)) {
			return new ArrayList<>();
		}

		List<ExcuterBean> result = new ArrayList<>();

		for (EmployeeIdentityView ei : eiList) {

			ExcuterBean bean = new ExcuterBean();

			bean.setExecuteAccountId(ei.getEmpId());
			bean.setExecuteId(ei.getEiId());
			bean.setExecuteName(ei.getEmpName());

			result.add(bean);
		}

		return result;
	}

	/**
	 * 过滤路径
	 * 
	 * @param activityDefinitionBean
	 */
	private void filterPath(ActivityDefinitionBean activityDefinitionBean) {
		// TODO filter path
	}

	private ActivityDefinitionBean getActivityDefinition(String processInstanceId, String taskId) throws LoraCoreException {

		ValidateUtil.isNull(processInstanceId, "processInstanceId");
		ValidateUtil.isNull(taskId, "taskId");

		// 获取流程实例
		ProcessInstance processInstance = this.getProcessInstanceById(processInstanceId);

		if (processInstance == null) {
			throw new LoraWorkflowException("cant find ProcessInstance processInstanceId=[" + processInstanceId + "]");
		}

		// 获取任务实例
		ProcessTask processTask = getProcessTaskById(taskId);

		if (processTask == null) {
			throw new LoraWorkflowException("cant find processTask taskId=[" + taskId + "]");
		}

		// 获取流程定义
		ProcessDefinitionBean pdEntity = processDefinitionService.getProcessDefById(processInstance.getProcessDefinitionId());

		// 获取 当前生效版本 当前任务的环节定义
		ActivityDefinitionBean activityDefinitionBean = processDefinitionService.getProcessDefinitionActivity(pdEntity,
				processTask.getActivityDefId(), processInstance.getProcessDefinitionVersion());

		return activityDefinitionBean;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.workflow.engines.IProcessMoveService#submit(java.lang.String
	 * , java.lang.String, java.util.List)
	 */
	@Override
	public boolean submit(String processInstanceId, String taskId, List<PathDefinitionBean> nextPathList) throws LoraCoreException {
		TaskInstanceBean taskInstance = getTaskInstance(processInstanceId, taskId, false, false);

		taskInstance.getActivityDefinitionBean().setNextPathList(nextPathList);

		return this.submit(taskInstance);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.biz.workflow.engines.IProcessMoveService#submit(org.lora.biz
	 * .workflow.engines.bean.TaskInstanceBean)
	 */
	@Override
	public boolean submit(TaskInstanceBean taskInstanceBean) throws LoraCoreException {
		// 提交参数校验
		submitValidate(taskInstanceBean);

		long currentTime = System.currentTimeMillis();
		BigDecimal currentTimeBigDecimal = new BigDecimal(currentTime);

		// 处理当前任务
		excuteCurrentProcessTask(taskInstanceBean, currentTimeBigDecimal);

		// 生成后续任务和taskExcute
		boolean isEnd = excuteNextTaskAndExcute(taskInstanceBean, currentTimeBigDecimal);

		// 流程结束的处理
		if (isEnd) {
			endProcess(taskInstanceBean.getProcessInstance(), currentTimeBigDecimal);
		}

		return isEnd;

	}

	protected void endProcess(ProcessInstance processInstance, BigDecimal currentTimeBigDecimal) {

		processInstance.setIsDelete(WorkflowConstant.PROCESS_DEL_STATE_YES);
		processInstance.setFinishTime(currentTimeBigDecimal);
		processInstance.setState(WorkflowConstant.PROCESS_STATE_FINISH);

		ProcessInstanceExample example = new ProcessInstanceExample();
		example.createCriteria().andIdEqualTo(processInstance.getId());

		processInstanceMapper.updateByExample(processInstance, example);

	}

	/**
	 * 提交校验
	 * 
	 * @param taskInstanceBean
	 * @throws LoraCoreException
	 */
	public void submitValidate(TaskInstanceBean taskInstanceBean) throws LoraCoreException {
		ValidateUtil.isNull(taskInstanceBean, "taskInstanceBean");

		List<PathDefinitionBean> nextPathList = taskInstanceBean.getActivityDefinitionBean().getNextPathList();

		// 验证路径是否为空
		if (ValidateUtil.isEmpty(nextPathList)) {
			throw new LoraWorkflowException("pathList is null processInstanceId=[" + taskInstanceBean.getProcessInstance().getId()
					+ "] taskId=[" + taskInstanceBean.getProcessTask().getTaskId() + "]");
		}

		// 验证路径处理人
		for (PathDefinitionBean entity : nextPathList) {
			if (!entity.isToEnd()) {
				List<ExcuterBean> handerList = entity.getToActivityDefinitionBean().getHanderList();
				if (ValidateUtil.isEmpty(handerList)) {
					String toActivityName = entity.getToActivityDefinitionBean().getActName();
					throw new LoraWorkflowException("handerList is null processInstanceId ["
							+ taskInstanceBean.getProcessInstance().getId() + "] taskId [" + taskInstanceBean.getProcessTask().getTaskId()
							+ "] toActivityName [" + toActivityName + "]");
				}
			}
		}

		// 验证流程状态
		String processState = taskInstanceBean.getProcessInstance().getState();
		if (StringUtil.isNull(processState)) {
			throw new LoraWorkflowException("processState is null processInstanceId=[" + taskInstanceBean.getProcessInstance().getId()
					+ "]");
		}

		if (processState.equals(WorkflowConstant.PROCESS_STATE_FINISH)) {
			throw new LoraWorkflowException("process have finish processInstanceId=[" + taskInstanceBean.getProcessInstance().getId() + "]");
		}

	}

	/**
	 * 处理当前任务
	 * 
	 * @param taskInstanceBean
	 */
	protected void excuteCurrentProcessTask(TaskInstanceBean taskInstanceBean, BigDecimal currentTimeBigDecimal) {

		ProcessTask processTask = taskInstanceBean.getProcessTask();

		processTask.setFinishTime(currentTimeBigDecimal);
		processTask.setTaskState(WorkflowConstant.TASK_STATE_HAVEDO);

		ProcessTaskExample example = new ProcessTaskExample();
		example.createCriteria().andTaskIdEqualTo(processTask.getTaskId());
		processTaskMapper.updateByExample(processTask, example);

	}

	/**
	 * 处理后续任务
	 * 
	 * @param taskInstanceBean
	 * @param currentTimeBigDecimal
	 */
	protected boolean excuteNextTaskAndExcute(TaskInstanceBean taskInstanceBean, BigDecimal currentTimeBigDecimal) {

		List<PathDefinitionBean> nextPathList = taskInstanceBean.getActivityDefinitionBean().getNextPathList();

		List<ProcessTask> processTaskList = new ArrayList<>();
		List<ProcessTaskExcute> processTaskExcuteList = new ArrayList<>();

		for (PathDefinitionBean pathDefinitionBean : nextPathList) {
			List<String> waitActIdList = pathDefinitionBean.getWaitActtivityId();

			if (ValidateUtil.isEmpty(waitActIdList)) {
				// 无需等待

				creatTaskAndTaskAndExcute(pathDefinitionBean, taskInstanceBean, currentTimeBigDecimal, processTaskList,
						processTaskExcuteList);

			} else {
				// 需要等待 TODO
			}
		}

		for (ProcessTask processTask : processTaskList) {
			processTaskMapper.insert(processTask);
		}

		for (ProcessTaskExcute processTaskExcute : processTaskExcuteList) {
			processTaskExcuteMapper.insert(processTaskExcute);
		}

		return isEndProcess(processTaskList, taskInstanceBean.getProcessInstance().getId());

	}

	/**
	 * 判断流程是否结束
	 * 
	 * @param newTaskList
	 * @param currentTaskId
	 * @return
	 */
	private boolean isEndProcess(List<ProcessTask> nextTaskList, String processInstanceId) {
		ProcessTaskExample example = new ProcessTaskExample();
		example.createCriteria().andProcessInstanceIdEqualTo(processInstanceId).andTaskStateEqualTo(WorkflowConstant.TASK_STATE_TODO);

		List<ProcessTask> taskList = processTaskMapper.selectByExample(example);

		if (ValidateUtil.isEmpty(taskList)) {// 没有待办状态的任务 肯定是结束了
			return true;
		} else if (taskList.size() > 1) {// 有待办状态的任务 并且大于1 无论如何都结束不了
			return false;
		} else {// 只有一个待办状态的任务

			boolean allDone = true;

			for (ProcessTask processTask : nextTaskList) {

				if (processTask.getTaskState().equals(WorkflowConstant.TASK_STATE_TODO)) {
					allDone = false;
					break;
				}
			}

			return allDone;
		}

	}

	protected void creatTaskAndTaskAndExcute(PathDefinitionBean pathDefinitionBean, TaskInstanceBean taskInstanceBean,
			BigDecimal currentTimeBigDecimal, List<ProcessTask> processTaskList, List<ProcessTaskExcute> processTaskExcuteList) {
		if (ValidateUtil.isEqual(WorkflowConstant.EXCUTE_MULTI_SERIAL, pathDefinitionBean.getMultiType())) {
			// 分组串行
			// TODO
		} else if (ValidateUtil.isEqual(WorkflowConstant.EXCUTE_SINGLE, pathDefinitionBean.getMultiType())) {
			// 单人处理

			List<ExcuterBean> list = pathDefinitionBean.getToActivityDefinitionBean().getHanderList();

			ProcessTask processTask = this.creatTask(taskInstanceBean, pathDefinitionBean, currentTimeBigDecimal, list.get(0));

			ProcessTaskExcute processTaskExcute = this.creatTaskExcute(taskInstanceBean, pathDefinitionBean, processTask.getTaskId());

			processTaskList.add(processTask);
			processTaskExcuteList.add(processTaskExcute);

		} else if (ValidateUtil.isEqual(WorkflowConstant.EXCUTE_MULTI_COMPETE, pathDefinitionBean.getMultiType())) {
			// 抢先处理
			// TODO
		} else if (ValidateUtil.isEqual(WorkflowConstant.EXCUTE_MULTI_PARALLEL, pathDefinitionBean.getMultiType())) {
			// 分组并行
			// TODO
		}

	}

	/**
	 * @param taskInstanceBean
	 * @param pathDefinitionBean
	 * @param currentTimeBigDecimal
	 * @param excuterBean
	 * @return
	 */
	protected ProcessTask creatTask(TaskInstanceBean taskInstanceBean, PathDefinitionBean pathDefinitionBean,
			BigDecimal currentTimeBigDecimal, ExcuterBean excuterBean) {

		ProcessTask processTask = new ProcessTask();

		String newTaskId = super.creatBeanId(WorkflowConstant.PREFIX_TASK);

		processTask.setActivityDefId(pathDefinitionBean.getToActivityDefinitionBean().getId());
		processTask.setActivityName(pathDefinitionBean.getToActivityDefinitionBean().getActName());
		processTask.setActivityNo(pathDefinitionBean.getToActivityDefinitionBean().getActNo());
		processTask.setCreateTime(currentTimeBigDecimal);
		processTask.setDomainId(taskInstanceBean.getProcessInstance().getDomainId());
		processTask.setIsDelete(WorkflowConstant.PROCESS_DEL_STATE_NO);
		processTask.setProcessInstanceId(taskInstanceBean.getProcessInstance().getId());
		processTask.setTaskId(newTaskId);

		if (pathDefinitionBean.isToEnd()) {
			processTask.setTaskState(WorkflowConstant.TASK_STATE_HAVEDO);
		} else {
			processTask.setExecuteId(excuterBean.getExecuteId());
			processTask.setExecuteAccountId(excuterBean.getExecuteAccountId());
			processTask.setExecuteName(excuterBean.getExecuteName());
			processTask.setExecuteType(excuterBean.getExecuteType());
			processTask.setGrantorAccountId(excuterBean.getGrantorAccountId());
			processTask.setGrantorId(excuterBean.getGrantorId());
			processTask.setGrantorName(excuterBean.getGrantorName());
			processTask.setGrantorReason(excuterBean.getGrantorReason());
			processTask.setGrantorType(excuterBean.getGrantorType());

			processTask.setTaskState(WorkflowConstant.TASK_STATE_TODO);
		}

		return processTask;

	}

	/**
	 * @param taskInstanceBean
	 * @param pathDefinitionBean
	 * @param toTaskId
	 * @return
	 */
	protected ProcessTaskExcute creatTaskExcute(TaskInstanceBean taskInstanceBean, PathDefinitionBean pathDefinitionBean, String toTaskId) {

		String taskExcuteId = super.creatBeanId(WorkflowConstant.PREFIX_TASK_EXCUTE);

		ProcessTaskExcute processTaskExcute = new ProcessTaskExcute();

		processTaskExcute.setDomainId(taskInstanceBean.getProcessInstance().getDomainId());
		processTaskExcute.setFromActivityDefId(taskInstanceBean.getProcessTask().getActivityDefId());
		processTaskExcute.setFromActivityName(taskInstanceBean.getProcessTask().getActivityName());
		processTaskExcute.setFromActivityNo(taskInstanceBean.getProcessTask().getActivityNo());
		processTaskExcute.setFromTaskId(taskInstanceBean.getProcessTask().getTaskId());
		processTaskExcute.setId(taskExcuteId);
		processTaskExcute.setIsDelete(WorkflowConstant.PROCESS_DEL_STATE_NO);
		processTaskExcute.setPathDefId(pathDefinitionBean.getId());
		processTaskExcute.setPathName(pathDefinitionBean.getPathName());
		processTaskExcute.setPathNo(pathDefinitionBean.getPathNo());
		processTaskExcute.setProcessInstanceId(taskInstanceBean.getProcessInstance().getId());
		processTaskExcute.setToActivityDefId(pathDefinitionBean.getToActivityDefinitionBean().getId());
		processTaskExcute.setToActivityName(pathDefinitionBean.getToActivityDefinitionBean().getActName());
		processTaskExcute.setToActivityNo(pathDefinitionBean.getToActivityDefinitionBean().getActNo());

		if (pathDefinitionBean.isToEnd()) {
			processTaskExcute.setToTaskId(WorkflowConstant.TASK_EXCUTE_END);
		} else {
			processTaskExcute.setToTaskId(toTaskId);
		}

		return processTaskExcute;

	}

}
